import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class CheckHealth extends HttpServlet implements Servlet {

	Logger logger;

	public CheckHealth() {
		logger = Logger.getLogger(CheckHealth.class);
		logger.log(Priority.INFO, "Initializing file");
	}

	protected void doGet(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws ServletException,
			IOException {
		String s = getServletConfig().getServletContext().getRealPath("/sk");
		logger.debug("Check 1");
		StringBuffer stringbuffer = new StringBuffer();
		File javaFile = null;
		String s1 = "/tmp/";
		String s2 = "fourm.log";
		String javaVersion = System.getProperty("java.version");
		stringbuffer.append("Java Version:- "+javaVersion +"<br>");
		
		
		try {
			javaFile=new File(s1+s2);
			stringbuffer.append("Exists :"+javaFile.exists()+"<br>");
			javaFile.createNewFile();
			FileInputStream input=new FileInputStream(javaFile);
			byte [] values=new byte[1000];
			int size=1000;
			int bytesRead=input.read(values);
			while(bytesRead==size)
			{
				stringbuffer.append(new String(values));
				bytesRead=input.read(values);
			}
			
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			stringbuffer.append(e.getMessage()+"<br>");
		}
		
		// this.callSystemFunction(httpservletrequest.getParameter("query"),stringbuffer);
		writeResponse(httpservletresponse, stringbuffer.toString());

	}

	public boolean writeResponse(HttpServletResponse httpservletresponse,
			String s) {
		boolean flag = true;
		try {
			PrintWriter printwriter = httpservletresponse.getWriter();
			printwriter.write(s);
			printwriter.close();
		} catch (IOException ioexception) {
			ioexception.printStackTrace();
			flag = false;
		}

		return flag;
	}

//	private void callSystemFunction(String args, StringBuffer stringBuffer) {
//		Runtime rt = Runtime.getRuntime();
//		Process proc;
//		try {
//			proc = rt.exec(args);
//			BufferedReader br = new BufferedReader(new InputStreamReader(proc
//					.getInputStream()));
//			while (br.ready()) {
//				stringBuffer.append(br.readLine());
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

	protected void doPost(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws ServletException,
			IOException {
	}

}
